using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Globalization;

namespace Microsoft.Samples.SqlServer
{
    /// <summary>
    /// Summary description for InstantOrder
    /// </summary>
    [WebService(Namespace = "http://schemas.microsoft.com/samples/sqlserver/storefront/2005/3/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class InstantOrder : System.Web.Services.WebService
    {

        public InstantOrder()
        {

        }

        [WebMethod(Description = "The OrderItem method enables a remote client to programmatically place an order using a WebService.", EnableSession = false)]
        public OrderDetails OrderItem(string userName, string password, int productID, int quantity,
            string addressLine1, string addressLine2, string addressCity, int addressStateProvinceID, string addressPostalCode)
        {

            // Login client using provided username and password
            CustomersDB accountSystem = new CustomersDB();
            CustomerDetails customerDetails = accountSystem.Login(userName, password);
            String customerId = customerDetails.CustomerID;

            if (customerId == null)
            {
                throw new Exception("Error: Invalid Login!");
            }

            // Wrap in try/catch block to catch errors in the event that someone types in
            // an invalid value for quantity
            int qty = System.Math.Abs(quantity);
            if (qty == quantity && qty < 1000)
            {

                // Add Item to Shopping Cart
                ShoppingCartDB myShoppingCart = new ShoppingCartDB();
                myShoppingCart.AddItem(customerId, productID, qty);
                bool isExtendedProcessing = false;
/* Reenable this code when new service broker components integrate with the Storefront sample

                     (ConfigurationSettings.AppSettings["ExtendedOrderProcessing"].ToUpper(WorldReady.USCulture) ==
                                             "TRUE");
*/

                int addressID =
                    accountSystem.AccessCustomerAddress(Int32.Parse(
                    customerId, CultureInfo.InvariantCulture), "Billing",
                                                                    new
                    CustomerAddress(0, addressLine1, addressLine2, addressCity,
                                    addressStateProvinceID, "", addressPostalCode));


                // Place Order
                OrdersDB orderSystem = new OrdersDB();
                int orderId = orderSystem.PlaceOrder(customerId, customerId,
                    // Use default shipper for instant ordering
                                                     1,
                                                     isExtendedProcessing ?
                                                     Constants.Status.
                                                     PendingInventoryCheck :
                                                     Constants.Status.Shipped,
                                                     addressID,
                    //Use the same address for billing and shipping
                    //for API simplicity reasons, and security.
                                                     addressID);

/* Reenable this code when new service broker components integrate with the Storefront sample

                // Demonstrate service broker features if ExtendedOrderProcessing is true in web.config file.
                if (isExtendedProcessing)
                {
                    ExtendedOrderProcessing eop = new ExtendedOrderProcessing();

                    eop.PostSalesOrder(orderId, customerId, orderSystem);
                }

*/


                // Return OrderDetails
                return orderSystem.GetOrderDetails(orderId, customerId);
            }
            else
            {
                // invalid input 
                return null;
            }
        }

        //*******************************************************
        //
        // InstantOrder.CheckStatus() Method 
        //
        // The CheckStatus method enables a remote client to programmatically
        // query the current status of an order in the AdventureWorks System. 
        //
        //*******************************************************  

        [WebMethod(Description = "The CheckStatus method enables a remote client to programmatically query the current status of an order in the AdventureWorks System.", EnableSession = false)]
        public OrderDetails CheckStatus(string userName, string password, int orderID)
        {

            // Login client using provided username and password
            CustomersDB accountSystem = new CustomersDB();
            CustomerDetails customerDetails = accountSystem.Login(userName, password);
            String customerId = customerDetails.CustomerID;

            if (customerId == null)
            {
                throw new Exception("Error: Invalid Login!");
            }

            // Return OrderDetails Status for Specified Order
            OrdersDB orderSystem = new OrdersDB();
            return orderSystem.GetOrderDetails(orderID, customerId);
        }
    }
}
